﻿using System;
using System.Collections.Generic;

namespace Kay.BaseLibrary
{
    /// <summary>
    /// 自定义方法比较器
    /// <para>用于 Linq 自定义比较两个对象</para>
    /// </summary>
    public class Comparer<T> : IEqualityComparer<T>
    {
        private readonly Func<T, T, bool> _func;

        /// <summary>
        /// 初始化比较器
        /// </summary>
        /// <param name="func">指定比较器使用的方法</param>
        public Comparer(Func<T, T, bool> func)
        {
            _func = func;
        }

        /// <inheritdoc />
        public bool Equals(T x, T y)
        {
            if (x == null && y == null)
                return true;
            if (x == null|| y == null)
                return false;

            return _func != null && _func(x, y);
        }

        /// <inheritdoc />
        public int GetHashCode(T obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
}
